package cn.tool.lwd;

import com.google.common.base.CaseFormat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author lwd
 * @date 2021/4/6 15:08
 */
public class ProjectUtil {

    private static FiltterReplaceCodeUtil filtterReplaceCodeUtil = new FiltterReplaceCodeUtil();

    private static HashMap concurrentModel = new HashMap();

    private static String inPathRoot = "in_h51Vz5sza8EnUbyslXSDvuXeutB57JYR";

    private static String outPathRoot = "out";

    private static String configFile = "param.properties";

    public static void main(String[] args) throws IOException {

        Properties properties = new Properties();
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = new FileInputStream(configFile);
        // 使用properties对象加载输入流
        properties.load(in);
        String inPathRoot1 = ProjectUtil.inPathRoot;
        String inPathRootConfig = properties.getProperty("inPathRoot");
        if(!"".equals(inPathRootConfig)){
            ProjectUtil.inPathRoot = inPathRootConfig;
        }
        filtterReplaceCodeUtil.init(inPathRoot);
        //获取key对应的value值
        for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
            String key = String.valueOf(objectObjectEntry.getKey());
            concurrentModel.put(key, objectObjectEntry.getValue());
            String value = String.valueOf(objectObjectEntry.getValue());
            if (key.equals("artifactId")) {
                concurrentModel.put("mainClassPrefix", CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, value.replaceAll("-", "_")));
                concurrentModel.put("artifactIdProjectPackageName", value.replaceAll("-", "."));
                concurrentModel.put("artifactIdProjectDirName", value.replaceAll("-", "/"));
            }
            if (key.equals("groupId")) {
                concurrentModel.put("groupIdDir", value.replaceAll("\\.", "/"));
            }
        }
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        outPathRoot = outPathRoot + df.format(new Date());
        File outDir = new File(outPathRoot);
        if (!outDir.exists()) {
            outDir.mkdir();
        }

        inFile(inPathRoot);
    }

    public static void inFile(String filePath) throws IOException {
        File file = new File(filePath);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        String absolutePath = file2.getAbsolutePath();
                        String contentByTemplate = filtterReplaceCodeUtil.getContentByTemplate(absolutePath, concurrentModel);
                        int startIndex = contentByTemplate.indexOf(inPathRoot) + inPathRoot.length();
                        String substring = contentByTemplate.substring(startIndex);
                        String outPath = outPathRoot + substring;
                        outDirectory(outPath);
                        inFile(absolutePath);
                    } else {
                        String tempName = file2.getAbsolutePath();
                        String contentByTemplate1 = filtterReplaceCodeUtil.getContentByTemplate(tempName, concurrentModel);
                        int startIndex = tempName.indexOf(inPathRoot) + inPathRoot.length();
                        String substring = tempName.substring(startIndex);
                        String outPath = outPathRoot + substring;
                        outPath = filtterReplaceCodeUtil.getContentByContent(outPath, concurrentModel);
                        outFile(contentByTemplate1, outPath);
                    }
                }
            }
        } else {
            System.out.println(filePath + "111文件不存在!");
        }
    }


    public static void outFile(String fileContent, String file) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(fileContent.getBytes(StandardCharsets.UTF_8));
        fileOutputStream.flush();
        fileOutputStream.close();
    }

    public static void outDirectory(String directoryPath) {
        File file = new File(directoryPath);
        if (!file.exists() || !file.isDirectory()) {//如果文件夹不存在
            file.mkdirs();//创建文件夹
        }
    }


}
